<?
/*
**2015-07-15
**芙蓉园对接
**User by pzp
*/
class FuRongYuan{
	
	public function __construct($UUlid){
		$this->fry_url = '113.140.89.10/api/';
		switch($UUlid){
			case 10405:
				$this->appkey = '5683f911fc5f7935';
				$this->app_secret = '3cd561241b699b7e';
			break;
			case 10414:
				$this->appkey = '392d95eacc672b6f';
				$this->app_secret = '5c94117a40f0a372';				
			break;		
			default:
			break;
		}
	}
	
	//获取产品信息
	public function Product_Get(){
		$action ='product/get';
		$param_array = array(
			"timestamp"=>time(),
			"appkey"=>$this->appkey,
		);
		$sign  =  $this->sign($param_array);
		$request_data = $this->FRY_post($param_array,$sign,$action);
		print_r($request_data );
	}
	
	//下单
	public function SubmitOrder($request_array){
		$action ='order/new';
		$param_array = array(
			"is_qrcode"=>0,
			"count"=>$request_array['UUtnum'],
			"order_uuid"=>$request_array['pftordernum'],
			"product_id"=>$request_array['uuid'],
			"contact_name"=>$request_array['pftordername'],
			"identity_card_number_list"=>$request_array['IDcar'],
			"cell_number"=>$request_array['UUordertel'],
			"timestamp"=>time(),
			"appkey"=>$this->appkey,
		);
		$sign = $this->sign($param_array);
		$submitorder = $this->FRY_post($param_array,$sign,$action);
		if($submitorder["code"] == 0000){ //下单成功
			$this->logfile($request_array['pftordernum']."|".$submitorder["code"]."|".$submitorder["msg"],"submit_success");
			return $submitorder["data"]["order_num"];
		}
		else{
			$this->logfile($request_array['pftordernum']."|".$submitorder["code"]."|".$submitorder["msg"],"submit_fail");
			return 101;
		}
	}
	
	//支付订单
	public function PayOrder($ordernum){
		$action = 'order/pay';
		$param_array = array(
			"order_num"=>$ordernum,
			"send_sms"=>0,
			"timestamp"=>time(),
			"appkey"=>$this->appkey,
		);
		$sign = $this->sign($param_array);
		$request_data = $this->FRY_post($param_array,$sign,$action);
		if($request_data["code"] == 0000){
			$this->logfile($ordernum,"pay_success");
			return 100;
		}
		else{
			$this->logfile($ordernum."|".$request_data["code"]."|".$request_data["msg"],"pay_fail");
			return 101;
		}
	}		
	//退款
	public function RefundOrder($modify_array){
		$action = "order/refund";
		$param_array = array(
			"order_num"=>$modify_array['APIOrdern'],
			"identity_card_number_list"=>$modify_array['idcard'],
			"timestamp"=>time(),
			"appkey"=>$this->appkey,
		);
		$sign = $this->sign($param_array);
		$request_data = $this->FRY_post($param_array,$sign,$action);
		if($request_data["code"] == 0000){
			$this->logfile($modify_array['APIOrdern'],'refund_success');
			return 100;
		}
		else{
			$this->logfile($modify_array['APIOrdern']."|".$request_data["code"]."|".$request_data["msg"],'refund_fail');
			return 101;
		}
	}
	
	//签名
	public function sign($param){
		ksort($param);
		$o=""; 
		foreach ($param as $k=>$v){ 
			$o.= "$k".$v; 
		} 
		$sign = strtolower(md5($o.$this->app_secret));
		return $sign;
	}
	
	//提交
	public function FRY_post($param_array,$sign,$action){
		$post_array = array(
			"sign"=>$sign,
			"param"=>$param_array,
		);
		$post["data"] = json_encode($post_array);
		$ch = curl_init();
		$url = $this->fry_url.$action;
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$url); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
		$reponse = curl_exec($ch);
		curl_close($ch);
		if($reponse){
			return json_decode($reponse,true);			
		}else{
			return $reponse;
		}
	}
	
	//日志
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt';
		if(file_exists($filepath)){
			$word = date("Y-m-d H:i:s")."|".$word;
			$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			{
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
				$handler=fopen($filepath,'w');
				flock($handler, LOCK_EX) ;
				fwrite($handler,"\n".$word);
				flock($handler, LOCK_UN);
				fclose($handler);
			}
			else
			{
				$fp = fopen('/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt',"a");
				flock($fp, LOCK_EX);
				fwrite($fp,$word."\n");
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}else{
			$fp = fopen('/var/www/html/new/d/logs/api/FuRongYuan/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}
// $fry = new FuRongYuan(10414);
// $get_product = $fry->Product_Get();
// $SubmitOrder = $fry->SubmitOrder();
// $PayOrder = $fry->PayOrder("2015071618491013764");
// $RefundOrder = $fry->RefundOrder();print_r($RefundOrder);
?> 